package com.yss.sofa.report.engine.json;

import java.io.IOException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.node.IntNode;

import com.yss.sofa.report.engine.series.BarLineSeries;
import com.yss.sofa.report.engine.series.CustomSeries;
import com.yss.sofa.report.engine.series.MultiPieSeries;
import com.yss.sofa.report.engine.series.Series;
import com.yss.sofa.report.engine.series.NormalBarSeries;
import com.yss.sofa.report.engine.series.NormalLineSeries;
import com.yss.sofa.report.engine.series.NormalPieSeries;
import com.yss.sofa.report.engine.series.SpiderWebSeries;
import com.yss.sofa.report.engine.series.StackAreaSeries;
import com.yss.sofa.report.engine.series.StackBarSeries;


public class SeriesDeserializer extends JsonDeserializer<Series>{

	public Series deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		Series series;
		JsonNode node = jp.getCodec().readTree(jp); 
		int type=(Integer) ((IntNode) node.get("type")).getNumberValue();  
		switch (type) {
		case Series.TYPE_CUSTOM:
			series=new CustomSeries();
			break;
		case Series.TYPE_NORMAL_BAR:
			series=new NormalBarSeries();
			break;
		case Series.TYPE_NORMAL_LINE:
			series=new NormalLineSeries();
			break;
		case Series.TYPE_NORMAL_PIE:
			series=new NormalPieSeries();
			break;
		case Series.TYPE_STACK_BAR:
			series=new StackBarSeries();
			break;
		case Series.TYPE_STACK_AREA:
			series=new StackAreaSeries();
			break;
		case Series.TYPE_BAR_LINE:
			series=new BarLineSeries();
			break;
		case Series.TYPE_MULTI_PIE:
			series=new MultiPieSeries();
			break;
		case Series.TYPE_SPIDER_WEB:
			series=new SpiderWebSeries();
			break;
		default:
			throw new JsonMappingException("series.type["+type+"]无效");
		}
		series.read(node);
		return series;
	}

}
